package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.LogManager;
import com.informationsystem.service.NewsManager;
import com.informationsystem.service.PictureNewsManager;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Log;
import com.informationsystem.model.News;
import com.informationsystem.model.PictureNews;
import com.informationsystem.model.Systemuser;
import com.informationsystem.webapp.action.BaseAction;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;

public class NewsAction extends BaseAction implements Preparable {
    private NewsManager newsManager;
    private PictureNewsManager picturenewsManager;
    private List newss;
    private News news;
    private Long id;
    private String query;
    private Date hourformat;
    
    public void setNewsManager(NewsManager newsManager) {
        this.newsManager = newsManager;
    }

    public List getNewss() {
        return newss;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String newsId = getRequest().getParameter("news.id");
            if (newsId != null && !newsId.equals("")) {
                news = newsManager.get(new Long(newsId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            newss = newsManager.search(query, News.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            newss = newsManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public News getNews() {
        return news;
    }

    public void setNews(News news) {
        this.news = news;
    }

    public String delete() {
    	news = newsManager.get(news.getId());
        newsManager.remove(news.getId());
        addLog("delete news id:" +news.getId() + " title: "+ news.getTitle());
        saveMessage(getText("news.deleted"));
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            news = newsManager.get(id);
        } else {
            news = new News();
            news.setSystemuser(getLoginUser());
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (news.getId() == null);
        
        if(!isNew){
        	news.setPostDate(generatePostDate());
        }
        else{
        	news.setPostDate(new Date());
        }

        
        news = newsManager.save(news);

        String key = (isNew) ? "news.added" : "news.updated";
        saveMessage(getText(key));
        
        if(!isNew){
        	addLog("edit news id:" +news.getId() + " title: "+ news.getTitle());
        }
        else{
        	addLog("add news id:" +news.getId() + " title: "+ news.getTitle());
        }
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public SystemuserManager getSystemuserManager() {
		return systemuserManager;
	}

	public void setSystemuserManager(SystemuserManager systemuserManager) {
		this.systemuserManager = systemuserManager;
	}
	
	public List getSystemuserList(){
		return systemuserManager.getAll();
	}

	public PictureNewsManager getPicturenewsManager() {
		return picturenewsManager;
	}

	public void setPicturenewsManager(PictureNewsManager picturenewsManager) {
		this.picturenewsManager = picturenewsManager;
	}
	
	public List getPictureNewsList(){
		List  pictureNewsList = picturenewsManager.getAll();
		return pictureNewsList;
	}
	
	public Date getHourformat() {
		return news.getPostDate();
	}

	public void setHourformat(Date hourformat) {
		this.hourformat = hourformat;
	}
	
	@SuppressWarnings("deprecation")
	public Date generatePostDate(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(news.getPostDate()); 
		cal.set(Calendar.HOUR_OF_DAY, hourformat.getHours());
		cal.set(Calendar.MINUTE,hourformat.getMinutes());

		return cal.getTime();
	}
	
	public String getContextPath(){
		return "";
	}
}